package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import utils.CalculateDate;
import utils.MakeExcelHelper;
import utils.PageBean;
import utils.QueryPackage;






import beans.Package;
import beans.PackageInfo;
import beans.SmsStatus;
import beans.Terminal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dao.PackageDao;
import dao.PackageInfoDao;
import dao.SmsStatusDao;

@SuppressWarnings("serial")
public class getUntakenParcel extends ActionSupport {

	private int terminalID;
	private int day;
	private String courierCompany;
	private int branchId;

	private static List<Package> packages = null;
	private List<Terminal> terminals = null;
	private String branchName;
	private PageBean<Package> unTakenParcel = null;
	private PageBean<Package> page = null;
	private int curpage;
	private int pageSize = 0;
	private int flag;
	private PackageDao packageDao = new PackageDao();
	private SmsStatusDao smsStatusDao = new SmsStatusDao();

	private PackageInfoDao packageInfoDao = new PackageInfoDao();

	private String termina;// 终端条件字符串
	private String status;// 包裹状态
	private Timestamp startstime; // 开始时间
	private Timestamp endtime; // 结束时间

	private Timestamp gettimestartstime; // 开始时间
	private Timestamp gettimeendtime; // 结束时间
	private String term; // 搜索条件
	private String sort; // 按什么排序

	private String waybillnum; // 运单编号
	private String packagenum;

	public String execute() throws Exception {
		packages = (List<Package>) packageDao.getUnTakenPackage(terminalID,
				day, courierCompany);
		unTakenParcel = new PageBean<Package>(0, 0, 0, packages);
		if (packages.size() > 0) {
			unTakenParcel.setCurpage(1);
			unTakenParcel.setPageSize(packages.size());
			unTakenParcel.setTotal(packages.size());
		}
		Date enddate = new Date();
		for (Package p : packages) {
			p.leftTime = String.valueOf(CalculateDate.getLeftTime(new Date(p
					.getStoreTime().getTime()), enddate));
			List<SmsStatus> list = smsStatusDao.getByParcelID(p);
			if (list.size() != 0) {
				p.msgStatus = list.get(0).getStatus();
			} else {
				p.msgStatus = 0;
			}
		}
		return "success";
	}

	/**
	 * 获取未取包裹Excel
	 * 
	 * @throws Exception
	 */
	public void getExcel() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		// packages = (List<Package>) packageDao.getUnTakenPackage(terminalID);
		// Timestamp timestamp = new Timestamp(new Date().getTime());
		// for (Package p : packages) {
		// p.leftTime = (timestamp.getTime() - p.getStoreTime().getTime())
		// / (1000 * 60 * 60 * 24) + "";
		// }
		MakeExcelHelper.MakeExcelByPackage(packages, response);
	}

	/**
	 * 分页及带条件查询package信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getPackage() throws Exception {
		ActionContext action = ServletActionContext.getContext();
		if (pageSize == 0) {
			pageSize = Integer.parseInt(action.getSession().get("pagesize")
					.toString());
		}
		QueryPackage querypage = null;
		if (flag == 1) {
			querypage = null;

			action.getSession().put("querypage", null);

			page = packageDao.getPackage(curpage, pageSize, querypage,
					branchId);
			terminals = packageDao.getAllTerminalOfBranchId(branchId);
			if (page.getList().size() > 0) {
				branchName = page.getList().get(0).getTerminal()
						.getBranch().getBranchName();
			}
			action.getSession().put("pack", page.getList());
			action.getSession().put("pagesize", pageSize);
			return "selectAllPackage";
		} else {
			if (action.getSession().get("querypage") != null)
				querypage = (QueryPackage) action.getSession().get("querypage");
			if (querypage == null) {
				querypage = new QueryPackage();
			}
			// 搜索
			if (this.term != null) {
				querypage.setTerm(this.term);
			}
			if (this.termina != null) {
				// 终端
				if (!this.termina.equals("")) {
					String[] temainalsid = termina.split("-");
					List<Integer> list = new ArrayList<Integer>();
					for (int i = 0; i < temainalsid.length; i++) {
						list.add(Integer.parseInt(temainalsid[i]));
					}
					querypage.setTerminal(list);
				}
				if (this.termina.equals(""))
					querypage.setTerminal(null);
			}
			// 包裹状态
			if (this.status != null) {
				if (!this.status.equals("")) {
					String[] statuslist = status.split("-");
					List<Integer> list = new ArrayList<Integer>();
					for (int i = 0; i < statuslist.length; i++) {
						list.add(Integer.parseInt(statuslist[i]));
						querypage.setGetTimeOfstart(null);
						querypage.setGetTimeOfend(null);
					}
					querypage.setStatus(list);
				}
				if (this.status.equals("")) {
					querypage.setStatus(null);
				}
			}
			if (this.startstime != null) {
				querypage.setDeposit(this.startstime);
				querypage.setEndtime(this.endtime);
			}
			if (flag == 5) {
				if (this.startstime != null) {
					querypage.setDeposit(this.startstime);
					querypage.setEndtime(this.endtime);
				} else {
					querypage.setDeposit(null);
					querypage.setEndtime(null);
				}
			}
			if (flag == 6) {
				if (this.gettimestartstime != null) {
					querypage.setGetTimeOfstart(this.gettimestartstime);
					if (this.gettimeendtime == null) {
						gettimeendtime = java.sql.Timestamp
								.valueOf(new SimpleDateFormat(
										"yyyy-MM-dd hh:mm:ss")
										.format(new Date()));
					}
					querypage.setGetTimeOfend(this.gettimeendtime);
				} else {
					querypage.setGetTimeOfstart(null);
					querypage.setGetTimeOfend(null);
				}
			}
			if (this.gettimestartstime != null) {
				querypage.setGetTimeOfstart(this.gettimestartstime);
				if (this.gettimeendtime == null) {
					gettimeendtime = java.sql.Timestamp
							.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
									.format(new Date()));
				}
				querypage.setGetTimeOfend(this.gettimeendtime);
			}
			// 排序

			if (this.sort != null) {
				if (this.sort.equals("")) {
					querypage.setSort(null);
				} else {
					querypage.setSort(this.sort);
				}
			}

			page = packageDao.getPackage(curpage, pageSize, querypage,
					branchId);
			terminals = packageDao.getAllTerminalOfBranchId(branchId);
			if (page.getList().size() > 0) {
				branchName = page.getList().get(0).getTerminal()
						.getBranch().getBranchName();
			}
			action.getSession().put("pack", page.getList());
			action.getSession().put("pagesize", pageSize);
			action.getSession().put("querypage", querypage);
			return "packagePages";
		}
	}

	/**
	 * 生成打印预览
	 * 
	 * @return
	 */
	public String printPackageInfo() {
		ActionContext action = ServletActionContext.getContext();
		QueryPackage querypage = (QueryPackage) action.getSession().get(
				"querypage");
		page = packageDao.getPackage(curpage, pageSize, querypage,
				branchId);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		List<String> list = new ArrayList<String>();// 获取打印的终端
		for (int i = 0; i < page.getList().size(); i++) {
			list.add(page.getList().get(i).getTerminal().getName());
		}
		for (int i = 0; i < list.size() - 1; i++) { // 循环遍历集体中的元素
			for (int j = list.size() - 1; j > i; j--) { // 这里非常巧妙，这里是倒序的是比较
				if (list.get(j).equals(list.get(i))) {
					list.remove(j);
				}
			}
		}
		if (querypage != null) {
			if (querypage.getStatus() != null) {
				if (querypage.getStatus().size() < 2) {
					if (querypage.getStatus().get(0).equals("0")) {
						action.getSession().put("status", "未取");
					} else {
						action.getSession().put("status", "已取");
						if (querypage.getGetTimeOfend() != null) {
							action.getSession().put(
									"endtime",
									df.format(querypage.getGetTimeOfstart())
											+ "-"
											+ df.format(querypage
													.getGetTimeOfend()));
						} else {
							action.getSession().put("endtime", "无");
						}
					}
				} else {
					action.getSession().put("status", "全部");
				}
			} else {
				action.getSession().put("status", "未取");
				action.getSession().put("endtime", "无");
			}
			if (querypage.getDeposit() != null) {
				action.getSession().put(
						"starttime",
						df.format(querypage.getDeposit()) + "-"
								+ df.format(querypage.getEndtime()));
			} else {
				Date date = new Date();
				action.getSession().put(
						"starttime",
						df.format(CalculateDate.getDateBefore(date, 2)) + "-"
								+ df.format(date));
			}
		} else {
			action.getSession().put("status", "未取");
			Date date = new Date();
			action.getSession().put(
					"starttime",
					df.format(CalculateDate.getDateBefore(date, 2)) + "-"
							+ df.format(date));
			action.getSession().put("endtime", "无");
		}
		Collections.sort(list);
		action.getSession().put("Terminal", list.toString());
		return "printPackageInfo";
	}

	/**
	 * 运单详情
	 * 
	 * @return
	 */
	public String getpPackageInfoOfNum() {
		List<PackageInfo> packageInfo = null;
		ActionContext action = ServletActionContext.getContext();
		action.getSession().remove("packageInfo");
		action.getSession().remove("packstatus");
		packageInfo = packageInfoDao.parcelIdTomobileNum(waybillnum);
		if (packageInfo.size() > 0) {
			action.getSession().put("packageInfo", packageInfo.get(0));
			if (packageInfo.get(0).getStatus().equals("0")) {
				action.getSession().put("packstatus", "未取");
			} else {
				action.getSession().put("packstatus", "已取");
			}
		}
		return "waybilldetails";
	}

	/**
	 * 获取用户名称
	 * 
	 * @return
	 * @throws IOException
	 */
	public String getPackageInfos() throws IOException {
		ArrayList<PackageInfo> list = packageInfoDao
				.parcelIdTomobileNum(packagenum);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if (list.size() > 0) {
			out.print(list.get(0).getConsigneeName());
		} else {
			out.print("");
		}
		out.close();
		return null;
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public List<Package> getPackages() {
		return packages;
	}

	public void setPackages(List<Package> packages) {
		getUntakenParcel.packages = packages;
	}

	public List<Terminal> getTerminals() {
		return terminals;
	}

	public void setTerminals(List<Terminal> terminals) {
		this.terminals = terminals;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String getCourierCompany() {
		return courierCompany;
	}

	public void setCourierCompany(String courierCompany) {
		this.courierCompany = courierCompany;
	}

	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public String getBranchName() {
		return branchName;
	}

	public void setBranchName(String branchName) {
		this.branchName = branchName;
	}

	public int getCurpage() {
		return curpage;
	}

	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getTermina() {
		return termina;
	}

	public void setTermina(String termina) {
		this.termina = termina;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Timestamp getStartstime() {
		return startstime;
	}

	public void setStartstime(Timestamp startstime) {
		this.startstime = startstime;
	}

	public Timestamp getEndtime() {
		return endtime;
	}

	public void setEndtime(Timestamp endtime) {
		this.endtime = endtime;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;

	}

	public String getWaybillnum() {
		return waybillnum;
	}

	public void setWaybillnum(String waybillnum) {
		this.waybillnum = waybillnum;
	}

	public String getPackagenum() {
		return packagenum;
	}

	public void setPackagenum(String packagenum) {
		this.packagenum = packagenum;
	}

	public Timestamp getGettimestartstime() {
		return gettimestartstime;
	}

	public void setGettimestartstime(Timestamp gettimestartstime) {
		this.gettimestartstime = gettimestartstime;
	}

	public Timestamp getGettimeendtime() {
		return gettimeendtime;
	}

	public void setGettimeendtime(Timestamp gettimeendtime) {
		this.gettimeendtime = gettimeendtime;
	}

	public PageBean<Package> getUnTakenParcel() {
		return unTakenParcel;
	}

	public void setUnTakenParcel(PageBean<Package> unTakenParcel) {
		this.unTakenParcel = unTakenParcel;
	}

	public PageBean<Package> getPage() {
		return page;
	}

	public void setPage(PageBean<Package> page) {
		this.page = page;
	}

}
